import thunk from 'redux-thunk'
import { createStore, applyMiddleware, combineReducers, compose } from 'redux'
import { reducer as homeReducer} from '../containers/Home/store'
import { reducer as siderReducer } from '../components/Sider/store/'
import { reducer as postArticleReducer } from '../containers/PostArticle/store/'
import { reducer as topHeaderReducer} from '../components/TopHeader/store/'
import { reducer as tagReducer} from '../containers/Tag/store/'

import clientAxios from '../client/request.js'
import serverAxios from '../server/request.js'

const reducer = combineReducers({
	home: homeReducer,
	sider: siderReducer,
	postArticle: postArticleReducer,
	topHeader: topHeaderReducer,
  tag: tagReducer
})




// 为了服务端渲染的时候不要多人访问共用一个store,所以封装成一个方法
export const getStore = (req) => {
  return createStore(reducer, applyMiddleware(thunk.withExtraArgument(serverAxios(req))))
}

export const getClientStore = () => {
  // 注水
  const axios = clientAxios
  const defaultState = window.context.state
  return window.__REDUX_DEVTOOLS_EXTENSION__ ? (
        createStore(reducer, compose(applyMiddleware(thunk.withExtraArgument(axios)), window.__REDUX_DEVTOOLS_EXTENSION__()))
    ) : (
        createStore(reducer, applyMiddleware(thunk.withExtraArgument(axios)))
    )
}
